package server;

import java.net.UnknownHostException;

import leaderElection.LeaderElection;
import centralRepository.CentralRepository;
import centralRepository.ServerObject;
import centralRepository.ServerObjectImpl;
import errorDetection.ErrorDetection;

public class SuperServer {
	
	private ServerObjectImpl so;

	private LeaderElection le;
	private ErrorDetection ed;
	private ActionsImpl ai;
	
	public SuperServer(int port, int edPortNum, int lePortNum, String serverName, boolean leader, ServerObjectImpl.Status status, String name, int id) {
		try {
			so = new ServerObjectImpl(port, edPortNum, lePortNum, serverName, leader, status, name, id, java.net.InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		CentralRepository.addProcess(so);
		CentralRepository.getAllProcesses();
		ed = new ErrorDetection(so);
		le = new LeaderElection(so);
		ai = new ActionsImpl(port, serverName, so);

		// wait for invocations from clients
		new Thread(ai).start();
		System.out.println(so.getName() + " STARTING");
	}
	
	public SuperServer(ServerObject sObject) {
		this.so=new ServerObjectImpl(sObject);
		CentralRepository.getAllProcesses();
		ed = new ErrorDetection(so);
		le = new LeaderElection(so);
		ai = new ActionsImpl(Integer.parseInt(so.getPortNum()), so.getName(), so);

		// wait for invocations from clients
		new Thread(ai).start();
		System.out.println(so.getName() + " STARTING");
	}
	
}
